import copy from 'copy-to-clipboard';
import { message } from 'antd';
export default function () {
  // 点击复制文本
  const copyText = (text: string) => {
    return new Promise((resolve, reject) => {
      try {
        //复制
        copy(text);
        message.success('复制成功');
        //下面可以设置复制成功的提示框等操作
        resolve(text);
      } catch (e) {
        //复制失败
        message.success('复制失败');
        reject(e);
      }
    });
  };

  //去除对象中为空的字段
  interface AnyObject {
    [key: string]: any;
  }
  const removeEmptyOrNull = <T extends AnyObject>(obj: T, keyToRemove?: keyof T): T => {
    const result = {} as any;

    for (const key of Object.keys(obj)) {
      if (key === keyToRemove) {
        // 如果当前 key 是要删除的 key，则不将其加入 result 中
        continue;
      }
      if (obj[key] != null && obj[key] !== '') {
        result[key] = obj[key];
      }
    }
    return result as T;
  };
  return {
    copyText,
    removeEmptyOrNull,
  };
}
